package main;

import java.io.IOException;
import java.net.*;

public class SimpleRTTServer {
	SimpleThread thread;
	ServerSocket serverSocket;
	public SimpleRTTServer() throws IOException {
		
		boolean listening = false;

        serverSocket = null;
        try {
            serverSocket = new ServerSocket(55555,0, InetAddress.getLocalHost());
            listening = true;
        } catch (IOException e) {
            System.err.println("Could not listen on port: 55555.");
            System.exit(-1);
        }

        //while (listening) {
            thread = new SimpleThread(serverSocket);
            thread.start();
        //}

        //serverSocket.close();
        
	}
	protected void finalize() throws Throwable{
		try {
			stopServer();
		} catch (IOException e) {
			System.err.println("Could not stop RTT server thread.");			
		}
	}
	public void stopServer() throws IOException{
		thread.interrupt();
		serverSocket.close();
		
	}
    public static void main(String[] args) throws IOException {

        boolean listening = false;

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(55555);
            listening = true;
        } catch (IOException e) {
            System.err.println("Could not listen on port: 55555.");
            System.exit(-1);
        }

        while (listening) {
            new SimpleThread(serverSocket).start();
        }

        serverSocket.close();

    }
}
